//
//  FlowListCell.swift
//  Employee
//
//  Created by swordjoy on 2017/6/28.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit

class FlowListCell: UITableViewCell {
  
  static func height(with model: FlowList) -> CGFloat {
    return 95 + UIFont.calculateHeight(width: UIScreen.width - 56, font: GlobalConfig.system16Font, text: model.title)
  }
  
  static var identifier: String { return "FlowListCell.swordjoy" }
  
//  Revoked("已撤销"), Finished("已完成"), NotPassed("未通过"), Auditing("审核中");
  
  var model: FlowList? {
    didSet {
      guard let temp = model else { return }
      if "Revoked" == temp.approverStatus {
        stateLabel.text = "已撤回"
        stateLabel.textColor = GlobalConfig.TextLightColor
      }
      else if "Finished" == temp.approverStatus {
        stateLabel.text = "已完成"
        stateLabel.textColor = GlobalConfig.TextLightColor
      }
      else if "NotPassed" == temp.approverStatus {
        stateLabel.text = "未通过"
        stateLabel.textColor = GlobalConfig.TextLightColor
      }
      else if "Auditing" == temp.approverStatus {
        stateLabel.text = "待" + temp.approvalName + "审批"
        stateLabel.textColor = GlobalConfig.BGSkyBlueColor
      }
      titleLabel.text = temp.title
      dateLabel.text = DateHelper.getFormatTime(timeInmillisecond: temp.createTime)
    }
  }

    var checkModel: FlowList? {
        didSet {
            guard let temp = checkModel else { return }
            // 这里不会有
            if "Revoked" == temp.approverStatus {
                stateLabel.text = "已撤回"
                stateLabel.textColor = GlobalConfig.TextLightColor
            }
            else if "Finished" == temp.approverStatus {
                stateLabel.text = "已完成"
                stateLabel.textColor = GlobalConfig.TextLightColor
            }
            else if "NotPassed" == temp.approverStatus {
                stateLabel.text = "未通过"
                stateLabel.textColor = GlobalConfig.TextLightColor
            }
            else if "Auditing" == temp.approverStatus {
                stateLabel.text = temp.flowType
                stateLabel.textColor = GlobalConfig.BGSkyBlueColor
            }
            titleLabel.text = temp.title
            dateLabel.text = DateHelper.getFormatTime(timeInmillisecond: temp.createTime)
        }
    }
  
  fileprivate var containerView: UIView!
  fileprivate var stateLabel: UILabel!
  fileprivate var titleLabel: UILabel!
  fileprivate var dateLabel: UILabel!
  
  // MARK: - life cycle
  override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    selectionStyle = .none
    backgroundColor = GlobalConfig.BGWhiteColor
    create()
  }
  
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
}

// MARK: - fileprivate

extension FlowListCell {
  
  fileprivate func create() {
    containerView = UIView()
    contentView.addSubview(containerView)
    containerView.backgroundColor = UIColor.white
    containerView.snp.makeConstraints { (make) in
      make.left.equalTo(10)
      make.right.equalTo(self).offset(-10)
      make.top.equalTo(self).offset(2.5)
      make.bottom.equalTo(self).offset(-2.5)
    }
    containerView.layer.cornerRadius = 3
    containerView.clipsToBounds = true
    
    stateLabel = UILabel()
    // MARK: - <#note#>
    stateLabel.text = "已撤回"
    containerView.addSubview(stateLabel)
    stateLabel.font = GlobalConfig.system15Font
    stateLabel.snp.makeConstraints { (make) in
      make.top.equalTo(containerView).offset(18)
      make.left.equalTo(containerView).offset(18)
      make.right.equalTo(containerView).offset(-18)
    }

    titleLabel = UILabel()
    titleLabel.text = "自定义测试数据记得删除"
    containerView.addSubview(titleLabel)
    titleLabel.font = GlobalConfig.system16Font
    titleLabel.numberOfLines = 0
    titleLabel.snp.makeConstraints { (make) in
      make.left.equalTo(containerView).offset(18)
      make.right.equalTo(containerView).offset(-18)
      make.top.equalTo(stateLabel.snp.bottom).offset(16)
    }
    
    dateLabel = UILabel()
    containerView.addSubview(dateLabel)
    dateLabel.font = GlobalConfig.system12Font
    dateLabel.text = "昨天 16:40"
    dateLabel.textColor = GlobalConfig.TextLightColor
    dateLabel.snp.makeConstraints { (make) in
      make.left.equalTo(containerView).offset(18)
      make.right.equalTo(containerView).offset(-18)
      make.top.equalTo(titleLabel.snp.bottom).offset(10)
    }
  }
  
}
